% Build a root struct with a dXparadigm and the four cohTimeLearn tasks
global ROOT_STRUCT

% who!
subject = 'STA';

% hardware, graphics, timing...

settings = struct(...
    'useASL', false,...
    'mouseMode', true,...
    'mute', false,...
    'usePMD', true,...
    'dotDir', [85 95],...
    'viewingTimes', {126 200 317 502},...
    'coherence', {55 54 89 57}); %percent value

practiceSettings = struct(...
    'useASL', false,...
    'mouseMode', true,...
    'mute',false,...
    'usePMD', true,...
    'dotDir', [85 95],...
    'viewingTimes', [126 200 317 502]);

trialsPerBlock = 150;  %%<----- IMPORTANT NUMBER (use 150)

    % randomize order of viewing time blocks
    practiceSettings.viewingTimes = ...
        practiceSettings.viewingTimes(randperm(length(practiceSettings.viewingTimes)));

% normal times: [100 159 252 400]
% extended times: [126 200 317 502]

% show viewing times in blocks or interleaved?
blockwise = true;
if blockwise
    
    % select blockwise cohTimeLearn tasks
    
    rind = randperm(4)
    tL = { ...
        'taskCohTimeLearn_interleaved_Practice', 1, {practiceSettings, 4}, ...
        'taskCohTimeLearn_cohSet',	1,  {settings(rind(1)), trialsPerBlock, 'A'}, ... %176
        'taskCohTimeLearn_cohSet',	1,  {settings(rind(2)), trialsPerBlock, 'B'}, ...
        'taskCohTimeLearn_cohSet',  1,  {settings(rind(3)), trialsPerBlock, 'C'}, ...
        'taskCohTimeLearn_cohSet',  1,  {settings(rind(4)), trialsPerBlock, 'D'}, ...
        };
    
    % run [practice, cohQuest, timeQuest, cohQuest] x number of viewing times
    repeatAll = 0;

else

    % select interleaved cohTimeLearn tasks
    tL = { ...
        'taskCohTimeLearn_interleaved_Practice',    1,  {settings, 4}, ...
        'taskCohTimeLearn_interleaved_cohQuest',    1,  {settings, 12}, ...
        'taskCohTimeLearn_interleaved_timeQuest',   1,  {settings, 12}, ...
        };

    % run [practice, cohQuest, timeQuest] x 2
    repeatAll = 0;

end

% init and set dXparadigm.screenMode to same
sMode = 'remote';
rInit(sMode);

% dXparadigm/runTasks will bomb if dir doesn't exist.
%FIRADir = ['/Users/lab/GoldLab/Data/cohTimeLearn/', subject];
FIRADir = sprintf('~/GoldLab/Data/cohTimeLearn/%s/FIRA', subject);
if ~exist(FIRADir)  %, 'dir')
    mkdir(FIRADir)
end

% where to put the output of this file, for loading later
%ROOTDir = '/Users/lab/GoldLab/Data/cohTimeLearn/roots';
ROOTDir = sprintf('~/GoldLab/Data/cohTimeLearn/%s/ROOT', subject);
if ~exist(ROOTDir, 'dir')
    mkdir(ROOTDir)
end

feedbackSelect = { ...
    'showPctGood',      false; ...
    'showNumGood',      false; ...
    'showGoodRate',     false; ...
    'showPctCorrect',   false; ...
    'showNumCorrect',   false; ...
    'showCorrectRate',  false; ...
    'showTrialCount',   false; ...
    'showMoreFeedback', true};
feedbackSelect = cell2struct(feedbackSelect(:,2), feedbackSelect(:,1), 1);

pName = ['cohTimeLearn_', subject];
rAdd('dXparadigm',      1, ...
    'name',                 pName, ...
    'screenMode',           sMode, ...
    'taskList',             tL, ...
    'taskOrder',            'blockTasks', ...
    'repeatAllTasks',       repeatAll, ...
    'iti',                  1.0, ...
    'saveToFIRA',           true, ...
    'FIRA_doWrite',         true, ...
    'FIRA_saveDir',         FIRADir, ...
    'FIRA_filenameBase',	subject, ...
    'ROOT_saveDir',         ROOTDir, ...
    'showFeedback',         true, ...
    'feedbackSelect',       feedbackSelect, ...
    'moreFeedbackFunction', @cohTimeLearnFeedback);

% load all tasks and save
ROOT_STRUCT.dXparadigm = loadTasks(ROOT_STRUCT.dXparadigm);
%bigName = fullfile(rGet('dXparadigm', 1, 'ROOT_saveDir'), pName);
%save(bigName, 'ROOT_STRUCT');

bigName = fullfile(rGet('dXparadigm', 1, 'ROOT_saveDir'), ...
    [subject datestr(rGet('dXparadigm', 'sessionTime'), 30) 'R']);
if exist(bigName)
    save(bigName, 'ROOT_STRUCT', '-append');
else
    save(bigName, 'ROOT_STRUCT');
end

% clean up
rDone
clear all